package com.example.servicedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.servicedemo.service.RandomService;

public class MainActivity extends AppCompatActivity {
    private static TextView textView;
    private static Handler handler = new Handler();
    private static double randomDouble;
    public static void UpdateGUI(double refreshDouble){
        randomDouble = refreshDouble;
        handler.post(refreshLabel);
    }
    private static Runnable refreshLabel = new Runnable() {
        @Override
        public void run() {
            //显示随机数
            textView.setText("随机数:" + randomDouble);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent randomServiceIntent= new Intent(this, RandomService.class);

        Button startButton = findViewById(R.id.button_start);
        Button stopButton = findViewById(R.id.button_stop);
        textView = findViewById(R.id.textView);
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startService(randomServiceIntent);
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopService(randomServiceIntent);
            }
        });
    }
}